$(function(){
	var flag = true;
	var urlStr = window.location.href,
		urlStr2 = urlStr.split("product_id=")
		productId = parseInt(urlStr2[urlStr2.length-1]),
		ratetype ="" ;

	var reviewList = $(".rev-list");
	var pageList = $(".products-review .page");
	var reviewLi = $(".review li");
	var selectLabel = $(".rev-select label");
	var littlePicSize = $(".little-pic li").size();
	var littleFlag = true;
	var returnFlag = 0;
	$("#salebtn").on("click", function(){
		_hmt.push(['_trackEvent', '付费加购PC', 'click', '付费加购PC']);
	});
	$(".products-intro #trybtn").on("click", function(){
		_hmt.push(['_trackEvent', '试用加购PC', 'click', '试用加购PC']);
	});
	$("img").lazyload({
		effect : "fadeIn"
	}); 

	/*详情页点击小图显示大图*/
	$(".little-pic li").on("click",function(){
		$(this).addClass("cur").siblings().removeClass("cur");
		var littleImgPath = $(this).find("img").attr("src");		//获取图标路径
		if(imgFlag == 0){
			if(littleImgPath.indexOf("thumb/")>0){
				littleImgPath = littleImgPath.split("thumb/");
				bigImgPath = littleImgPath[0] + littleImgPath[1];
			}else{
				bigImgPath = littleImgPath;
			}
		}else{
			bigImgPath = littleImgPath.replace("thumb/","compress/");
		}
		
		$(".big-pic img").attr("src",bigImgPath);
	});
	$(".little-pic li").eq(0).click();
	//小图左右滚动
	if(littlePicSize>5){
		$(".little-pic .next").removeClass("grey");
		$(".little-pic .prev").click(function(){
			if(!littleFlag){
				$(".little-pic ul").animate({"left":0});
				$(this).addClass("grey");
				$(".little-pic .next").removeClass("grey");
				littleFlag = true;
			}
		
		})
		$(".little-pic .next").click(function(){
			if(littleFlag){
				$(".little-pic ul").animate({"left":-359+"px"});
				$(this).addClass("grey");
				$(".little-pic .prev").removeClass("grey");
				littleFlag = false;
			}
			
		})
	}

	
	//商品数量编辑器
	$(document).on("click", ".amount-reduce", function(){
		var input = $(this).next("input");
		if(input.data("disabled")||$(this).is(".dis")){
			return false;
		}else{
			var m = input.val()||0;
			m--;
			input.data("amount", m).trigger("change");
		}		

	}).on("click", ".amount-add", function(){
		var input = $(this).prev("input");
		if(input.data("disabled")||$(this).is(".dis")){
			return false;
		}else{
			var m = input.val()||0;
			m++;
			input.data("amount", m).trigger("change");
		}
	}).on("change", ".amount-input", function(){
		var _this = this;
		var max = parseInt($(this).attr("size"))||199;
		var min = 1;
		var state = true;
		var m = $(this).data("amount");//点加减号运算后的数量值，存储起来，等请求响应后显示
		var lastVal = $(this).data("lastVal");
		if($.isEmpty(m)||m===false){
			m = $(this).val()
		}else{
			m = parseInt(m);
		}
		if(m<min||m>max||isNaN(m)){
			state = false;
			m = lastVal;
		}
		if(m<max||m>min){
			$(this).siblings().removeClass("dis");
		}
		if(m==max){
			$(this).next().addClass("dis");
		}
		if(m==min){
			$(this).prev().addClass("dis");
		}
		$(_this).val(m).data({"disabled":false,"amount":false,"lastVal":m})
	});
	//初始化商品数量、金额小计
	$(".amount-input").trigger("change");


	//倒计时
	rDate($(".inxt-time em"),autodate);

	//tab切换
	$("dl.tab dt").on("click",function(){
		var n = $(this).index();
		$(this).addClass("cur").siblings().removeClass("cur");
		$(".products-detail dd").hide();
		$(".products-detail dd").eq(n).show();
		if(flag){
			$.doAjax({
				url:requestReviewUrl,
				data:{product_id:productId,backRatetype:""},
				success: function(data){
				flag = false;
				if(data.text != ""){
					reviewList.html(data.text);
					pageList.html(data.page);
				}else{
					reviewList.html("<p class='none'>暂无评论</p>");
				}
				if(data.backRate){
					if(data.backRate.a !=false){
						$(".percent .bigger em").html(data.backRate.a);
					}					
					reviewLi.eq(0).find("em").css({"width":data.backRate.a+"%"});
					reviewLi.eq(1).find("em").css({"width":data.backRate.b+"%"});
					reviewLi.eq(2).find("em").css({"width":data.backRate.c+"%"});
				}
				if(data.backNum){
					selectLabel.eq(1).find("em").html(data.backNum.a);
					selectLabel.eq(2).find("em").html(data.backNum.b);
					selectLabel.eq(3).find("em").html(data.backNum.c);
				}
				
			}
			})
		}
		
	})

	//好中差评价单选
	$(".rev-select input").on("click",function(){
		ratetype = $(this).val();
		$.doAjax({
			url: requestReviewUrl,
			data:{product_id:productId,backRatetype:ratetype},
			success: function(data){
				if(data.text != ""){
					reviewList.html(data.text);
					pageList.html(data.page);
				}else{
					reviewList.html("<p class='none'>暂无评论</p>");
				}
			}
		})
		
	})

	$(".buy .go").on("click", function(){
		if($(this).hasClass("icon-btn1")&&returnFlag !=1){
			var goodsId =$(this).parents(".intro-text").find('h1').attr("data-id");//parseInt($("h1").attr("data-id"));
			var number = parseInt($(".amount-input").val());
			$.doAjax({
				url: addCarUrl,
				type:"post",
				data:{
					goods_id:goodsId,
					product_id:saleid,
					number:number
				},
				success: function(data){
					if(data.flag){
						var cartNum = parseInt($("#sidebar .sidebar-cart .icon-circle").text());
						var addNum = parseInt($(".amount-input").val());
						$("#sidebar .sidebar-cart .icon-circle").text(cartNum+addNum);
						$.confirm(["温馨提示","商品已加入购物车","继续购物","去结算"],"",function(){
							window.location.href = goPayUrl;
						});
					}else{
						if(data.errCode == "4001"){
							$.loginPopup(getLoginUrl, loginUrl, codeUrl);
						}else{
							$.alert(data.msg);
						}	
					}
				}
			})
		}	
	});
	$(".buy .try").on("click", function(){
		if($(this).hasClass("icon-btn1")&&returnFlag !=1){
			var goodsId =$(this).parents(".intro-text").find('h1').attr("data-id");
			var number = parseInt($(".amount-input").val());
			$.doAjax({
				url: addCarUrl,
				type:"post",
				data:{
					goods_id:goodsId,
					product_id:tryid,
					number:number
				},
				success: function(data){
					if(data.flag){
						var cartNum = parseInt($("#sidebar .sidebar-cart .icon-circle").text());
						var addNum = parseInt($(".amount-input").val());
						$("#sidebar .sidebar-cart .icon-circle").text(cartNum+addNum);
						$.confirm(["温馨提示","商品已加入购物车","继续购物","去结算"],"",function(){
							window.location.href = goPayUrl;
						});
					}else{
						if(data.errCode == "4001"){
							$.loginPopup(getLoginUrl, loginUrl, codeUrl);
						}else{
							$.alert(data.msg);
						}	
						
					}
					
				}
			})
		}	
	});
	
	$(document).on('click','.products-review .page li a',function(e){
		e.preventDefault();
		var pageUrl = $(this).attr("href");
		$(this).addClass(".active").siblings().removeClass("active");
		if(pageUrl){
			$.doAjax({
				url: pageUrl,
				success: function(data){
					if(data.text != ""){
						reviewList.html(data.text);
						pageList.html(data.page);
					}else{
						reviewList.html("<p class='none'>暂无评论</p>");
					}
				}
			})
		}
	});
	//点击商品规格，页面跳转
	$(".intxt-kuang").on("click",".intxt-value span",function(){
		var sku1,sku2;
		var thisSpan = $(this);
		if($(this).hasClass("grey")){
			return false;
		}else{
			$(this).addClass("cur").siblings().removeClass("cur");
			if(thisSpan.parents(".intxt-kuang").hasClass("guige")){
				sku1 = $(this).attr("data-sku");
				sku2 = $(".guige-child span.cur").attr("data-sku");	
			}else{
				sku2 = $(this).attr("data-sku");
				sku1 = $(".guige span.cur").attr("data-sku");	
			}
			$.doAjax({
				url: skuUrl,
				data:{sku1:sku1,sku2:sku2},
				success: function(data){
					returnFlag = data.flag;
					if(data.flag == 0){
						window.location.href=goodUrl+data.data;
					}else{
						$(".guige-child").html(data.desc).css({"background":"#f2edf5"});
					}
				}
			})
		}
		
	})
	//打开评论列表图片大图
	$(".rev-list").on("click",".img-list img",function(){
		$.viewImage($(this).index(), $(this).siblings().andSelf().map(function(){return $(this).attr("src")}).get());
	})
})
function getDataId(){
	var str = "";
	str += "&product_id="+$("[data-id]").attr("data-id");
	str += "&data-spec1="+$("[data-spec1]").attr("data-spec1");
	str += "&data-sku1="+$("[data-sku1].cur").attr("data-sku1");
	str += "&data-spec2="+$("[data-spec2]").attr("data-spec2");
	str += "&data-sku2="+$("[data-sku2].cur").attr("data-sku2");
	str += "&try_id="+tryid;
	str += "&sale_pid="+saleid;
	return str;
}